var app = getApp();
Page(Object.assign({}, {
    data:{
        inited : false,
        shop : {},
        showqun : false,
        page : {
            isend : false,
            doing : false,
            doo : 'admin',
            pdata : {
                op : 'givelog',
                page : 1,
                status : 0,
                isint : 1,
            },
            waitf : 0,
            nodataf : 0,
        },
        datalist : [],
        uid : 0,
        stype : 1,
		siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
    },
    onLoad:function(options){
        var self = this;
        
        self.setData({
            datalist : [],
            'page.pdata.page' : 1,
            'page.isend' : false,
            'page.doing' : false,
        })

        app.com.auth(function(user){
            app.com.getPage(self,self.data.page,0,function(res){
                var data = res.data.data;
                self.setData({
                   datalist : data.list,
                   params : data.params,
                   shop : data.shop,
                   inited : true,
                   'page.pdata.isint' : 0,
                });
				wx.setNavigationBarTitle({
					title:  '赠送'+data.params.jfbname,
				});
				
            },false,function(res){
                app.com.alert( res.data.message,function(){
                    if( res.data.errno == 2 ){
                        wx.navigateTo({
                            url : res.data.data.url
                        });
                    }
                });
            });
        });

    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: self.data.sharedata.title,
            path: '',
        }
    },
    onPullDownRefresh: function () {
        this.onLoad();
        // 关闭下拉刷新
        wx.stopPullDownRefresh();
    },
    addthis : function(e){
        this.setData({
            money : e.currentTarget.dataset.m
        })
    },
    confirm(e){
        var self = this;
        
        if( self.data.money*1 <= 0 || !self.data.money ) {
            app.com.toast('请填写增加数量');
            return false;
        }
        var postdata = {
            op:'givejifen',
            money : self.data.money,
            type : self.data.stype,
            uid : self.data.uid,
        };

        app.com.confirm('提交后将扣除相应的'+self.data.params.jfbname+'，确定要提交吗？',function(){

            if( self.data.payed ) return false;
            self.data.payed = true;
            app.com.http('admin','POST',postdata,0,true,'','',function(res){
                if (!res.data.errno) {
                    app.com.toast( res.data.message );

                    if( self.data.stype == 1 ){
                        wx.previewImage({
                            current: res.data.data.url,
                            urls: [res.data.data.url]
                        })
                    }
                    self.setData({
                        'shop.jifen' : self.data.shop.jifen*1 - self.data.money*1,
                        isshowgive : false,
                    })
                    var pages = getCurrentPages();
                    var beforePage = pages[pages.length - 2];
                    if( beforePage ){
                        beforePage.onLoad();
                    }
                    
                }else{
                    app.com.toast( res.data.message );
                    if( res.data.errno == 2 ){
                        wx.navigateTo({
                            url : res.data.data.url
                        });
                    }              
                }
                self.data.payed = false;
            });
        })
    },
    money : function(e){
        this.setData({
            money : e.detail
        })
    },
    uidfn(e){
        this.setData({
            uid : e.detail
        })
    },
    showgive(){
        this.setData({
            isshowgive : !this.data.isshowgive
        })
    },
    radio(e){
        this.setData({
            stype : e.currentTarget.dataset.type
        })
    },
    onChange(e){
        var status = e.detail.index;
        var self = this;

        if( self.data.ising || status == self.data.page.pdata.status ) return false;
        self.data.ising = true;

        self.setData({
            datalist : [],
            'page.pdata.status' : status,
            'page.pdata.page' : 1,
            'page.isend' : false,
            'page.waitf' : 0,
            'page.nodataf' : 0
        });

        app.com.getPage(self,self.data.page,20,function(res){
            self.setData({
                datalist : res.data.data.list,
            });
        });
        self.data.ising = false;
    },
    viewqr(e){
        var self = this;
        var postdata = {
            op:'viewqr',
            id : e.currentTarget.dataset.id,
        };

        app.com.http('admin','POST',postdata,0,true,'','',function(res){
            if (!res.data.errno) {
                wx.previewImage({
                    current: res.data.data.url,
                    urls: [res.data.data.url]
                })
            }else{
                app.com.toast( res.data.message );
                if( res.data.errno == 2 ){
                    wx.navigateTo({
                        url : res.data.data.url
                    });
                }
            }
        });
    },
    onReachBottom : function(){
        var self = this;
        app.com.getPage(self,self.data.page,0,function(res){
            self.setData({
                datalist : self.data.datalist.concat(res.data.data.list)
            });
        });
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e,this.data.tid );
    },
    redirectto : function( e ){
        app.com.redirectto( e,this.data.tid );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    showimages : function( e ){
        app.com.showimages( e );
    }
}))